import {_decorator, Component, instantiate, Node, Prefab, resources, director} from 'cc';
import {logUtil} from "../../framework/util/logUtil";
import {Pools} from "../../framework/entity/ao/Pools";
import {CoinManager} from '../../framework/manager/CoinManager';
import {Constant} from "../../config/Constant";
import {AudioManager} from "../../framework/manager/AudioManager";

const {ccclass} = _decorator;

@ccclass('CoinCon')
export class CoinCon extends Component {
    constructor(propPath: string) {
        super();
        this.scene = director.getScene();
        this.propPath = propPath;
    }

    //日志控制器
    log = logUtil.instance
    logLevel = "CoinCon"

    propPath: string;
    propPools: Pools<string, Node> = new Pools();
    //预制体名称
    prefabName = "G_Idle"
    //场景
    scene

    /**
     * 加载敌人模型
     */
    init(): Promise<any> {
        return new Promise((resolve) => {
            resources.loadDir(this.propPath,Prefab, (err,result) => {
                for (let addressable of result) {
                    this.createPropPool(addressable);
                }
                resolve(result); // 所有资源加载完成后，解决Promise
            });

            this.log.log(this.logLevel, `加载结束`);
        });
    }

    /**
     * 敌人预制体处理
     */
    private createPropPool(prefab: Prefab) {
        //预制怪物对象配比
        const propCount = 1;
        // this.enemyNames.push(prefab.name);
        this.propPools.newPool(
            prefab.name,
            (): Node => {
                let node = instantiate(prefab!);
                node.addComponent(CoinManager)
                node.active = false;
                return node;
            },
            propCount,
            (node: Node) => {
                node.removeFromParent();
            }
        );
    }

    createCoin(node: Node) {
        let prop = this.propPools.allocc(this.prefabName);
        prop.active = true;
        let coinProperty = prop.getComponent(CoinManager).coinProperty
        coinProperty.isCollectible = true

        prop.setPosition(node.position)
        node.parent.addChild(prop)
        prop.once(Constant.EVENT_NAME.onCoinCollect, this.onCoinCollect, this);
        AudioManager.instance.playSfx(Constant.DYNAMIC_RESOURCE.Audio.AudioName.SfxOther003);

        this.log.log(this.logLevel, `createCoin`)
    }

    onCoinCollect(coinPropertyNode: Node) {
        this.log.log(this.logLevel, `onCoinCollect`)
        this.log.log("fight", `自动拾取2`)
        this.scheduleOnce(() => {
            AudioManager.instance.playSfx(Constant.DYNAMIC_RESOURCE.Audio.AudioName.SfxOther002);

            let coinProperty = coinPropertyNode.getComponent(CoinManager).coinProperty
            coinProperty.isCollectible = false
            coinProperty.dropTime = 0
            coinPropertyNode.active = false;
            this.propPools.free(this.prefabName, coinPropertyNode);

            //资源更新
            this.scene.getChildByPath(Constant.STATIC_NODE_PATH.UIGame).emit(Constant.EVENT_NAME.onResourceUpdate, 0, 1, false)
        }, 0.1)
    }
}


